import "../utils/howler.min.js";

const audioList = [];
export default function (src) {
    //console.log('src:',src);
    let myaudio = new Howl({
        preload: true,
        loop: false,
        src: src,
    });
    //console.log('myaudio:',myaudio);
    /* 将音频对象存到数组 */
    audioList.push(myaudio);
    /* 播放音频 */
    myaudio.playAudio = function(){
        /* 将所有其他播放器暂停 */
        audioList.forEach(item=>{
            item.stop();
        });
        myaudio.play();
    }
    /* 
    移除播放器,在unmounted方法里面一定要移除播放器,否则播放器
    会一直占用内存空间
     */
    myaudio.removeAudio = function(){
        /* 获取当前播放器的索引 */
        let index = audioList.indexOf(myaudio);
        if(index !== -1){
            /* 移除当前播放器 */
            audioList.splice(index,1);
        }
    }
    //console.log('audioList:',audioList.length);
    return myaudio;
}